{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "60003aff",
   "metadata": {},
   "source": [
    "#  资料补充 关于：list 切片例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f91d985d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 资料关于：list 切片\n",
    "alist = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    "\n",
    "print(alist[::])  # 返回包含原列表所有元素的新列表\n",
    "print(alist[::-1])  # 返回原列表的一个逆序列表\n",
    "print(alist[::2])  # [1, 3, 5, 7, 9] .取列表下标偶数位元素\n",
    "print(alist[1::2])  # [2, 4, 6, 8]  取列表下标奇数位元素\n",
    "print(alist[3:6])  # [4, 5, 6]  #取列表中下标3到6的值,步长是1\n",
    "print(alist[3:6:2])  # [4, 6] #取列表中下标3到6的值,步长是2\n",
    "\n",
    "print(alist[:10])  # [1, 2, 3, 4, 5, 6, 7, 8, 9]  . end大于列表长度时,取列表中所有元素,省略了步长1.\n",
    "print(alist[10:]) # []  . 表示从列表的第10位开始取,一直取到列表结果,步长是1.\n",
    "\n",
    "alist[len(alist):] = [10]  # 在列表的未尾添加一个元素\n",
    "print(alist) #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "\n",
    "alist[:0] = [-1,0]  # 在列表的头部插入元素. alist[:0] 相当于 alist[0:0] 再相当于 alist[0:0:1]\n",
    "print(alist) #[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "\n",
    "alist[5:5] = [-6]  # 把列表的第6个位置替换成-6. 相当于alist[5:5:1] = [-6]\n",
    "print(alist) #[-1, 0, 1, 2, 3, -6, 4, 5, 6, 7, 8, 9, 10]\n",
    "\n",
    "alist = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    "alist[:3] = ['A','B','C','D']  # 把alist的前3个元素换成['A','B','C','D']\n",
    "print(alist)  #['A', 'B', 'C', 'D', 4, 5, 6, 7, 8, 9]\n",
    "\n",
    "alist[3:] = ['E','F','G']  # 把列表第3个元素之后的元素替换成['E','F','G']\n",
    "print(alist) # ['A', 'B', 'C', 'E', 'F', 'G']\n",
    "\n",
    "alist = [1, 2, 3, 4, 5, 6]\n",
    "alist[::2] = [0]*3 # 替换列表下标是偶数的前三个元素,注意使用该方法时,列表下标是偶数的元素个数必须与后面替换的元素个数相等,否则报错\n",
    "print(alist) #[0, 2, 0, 4, 0, 6]\n",
    "\n",
    "alist[::2] = ['a','b','c'] # 跟上面一样的道理,只是替换的内容不一样\n",
    "print(alist) # ['a', 2, 'b', 4, 'c', 6]\n",
    "\n",
    "alist = [1, 2, 3, 4, 5, 6]\n",
    "alist[:3] = [] # 删除列表中前3个元素\n",
    "print(alist) # [4, 5, 6]\n",
    "\n",
    "\n",
    "alist = [1, 2, 3, 4, 5, 6]\n",
    "del alist[:3]  # 使用del关键字删除列表前3个元素\n",
    "print(alist) #[4, 5, 6]\n",
    "\n",
    "alist = [1, 2, 3, 4, 5, 6]\n",
    "del alist[::2]  # 删除列表元素,下标是偶数位的元素\n",
    "print(alist) #[2, 4, 6]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1dd3723e",
   "metadata": {},
   "source": [
    "# 列表的基本方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "218c718d",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "stu_id = [101,102,103,104,1085,106]\n",
    "# 思考，如何取出所有位置101的index？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f7b596a",
   "metadata": {},
   "source": [
    "## 增加 append()  增加在list的末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "930dabcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# names.append(1)\n",
    "# print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c3e42d5",
   "metadata": {},
   "source": [
    "## inser()增加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9344ec91",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "insert之后的names： ['Mike', 'yougege', 'yougege', 'Mary', 'Jan', 'Jack']\n"
     ]
    }
   ],
   "source": [
    "names.insert(1,'yougege')\n",
    "print(\"insert之后的names：\",names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea40b3e6",
   "metadata": {},
   "source": [
    "## 删除 remove  和 pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b4c98e52",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nRemove first occurrence of value.\\nRaises ValueError if the value is not present.\\n'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 优势：如果删除list中多个元素时，remove比较合适\n",
    "##\n",
    "\"\"\"\n",
    "Remove first occurrence of value.\n",
    "Raises ValueError if the value is not present.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "0fd9a4c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remove删除的结果： ['Mike', 'yougege', 'yougege', 'Mary', 'Jan', 'Jack']\n"
     ]
    }
   ],
   "source": [
    "# remove()一般情况下 使用remove先用in做判断\n",
    "if 101 in names:\n",
    "    names.remove(101)\n",
    "print(\"remove删除的结果：\",names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7b08d709",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pop()方法后的names: ['Mike', 'yougege', 'yougege', 'Mary', 'Jan']\n"
     ]
    }
   ],
   "source": [
    "# 2.2 pop() 删除位置为1的这个元素\n",
    "names.pop(-1)\n",
    "print(\"pop()方法后的names:\",names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7813ad1",
   "metadata": {},
   "source": [
    "## index()索引 查看该元素如10001 在stu_id中第一个出现的位置索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "6ce21c9a",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'stu_id' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-23-a21a0f271b86>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 查看该元素如10001 在stu_id中第一个出现的位置索引\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstu_id\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m101\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'stu_id' is not defined"
     ]
    }
   ],
   "source": [
    "# 查看该元素如10001 在stu_id中第一个出现的位置索引\n",
    "print(stu_id.index(101))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e5b7cd5",
   "metadata": {},
   "source": [
    "## clear()清除所有数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "19e4840a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "清除names列表后的值： []\n"
     ]
    }
   ],
   "source": [
    "names.clear()\n",
    "print(\"清除names列表后的值：\",names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75541041",
   "metadata": {},
   "source": [
    "## count()查看列表元素出现的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f618d417",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'stu_id' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-25-6bcc8dcf7fda>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# （简单应用）统计分数出现的次数\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"count方法的结果:\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mstu_id\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcount\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m101\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnames\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'stu_id' is not defined"
     ]
    }
   ],
   "source": [
    "# （简单应用）统计分数出现的次数\n",
    "print(\"count方法的结果:\",stu_id.count(101))\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7923c50f",
   "metadata": {},
   "source": [
    "## extend() 是给指定的list进行扩充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "bb5e4868",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'stu_id' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-27-f24004f9bf97>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnames\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mextend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstu_id\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnames\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'stu_id' is not defined"
     ]
    }
   ],
   "source": [
    "names.extend(stu_id)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e06d4185",
   "metadata": {},
   "source": [
    "## 列表的其他方法(Sequence Types 的一些方法)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "fe87a018",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "names的长度/names的列表元素个数： 0\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(\"names的长度/names的列表元素个数：\",len(names))\n",
    "## 2. in 查看元素是否存在list当中,结果为布尔值，False/True,\n",
    "##    一般在操作list的修改和删除时会先判断\n",
    "print(\"zhichao\" in names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c32b6a8",
   "metadata": {},
   "source": [
    "# 列表方法应用与总结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86a79997",
   "metadata": {},
   "source": [
    "## 增加 append() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "099aba18",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第三个元素为： 1997\n",
      "更新后的第三个元素为： 2001\n",
      "更新后的列表： ['Google', 'taobao', 'Runood', 'Baidu']\n"
     ]
    }
   ],
   "source": [
    "# 追加元素\n",
    "list = ['Google','taobao',1997,2000]\n",
    "print(\"第三个元素为：\",list[2])\n",
    "list[2] = 2001\n",
    "print(\"更新后的第三个元素为：\",list[2])\n",
    "list1 = ['Google','taobao','Runood']\n",
    "list1.append('Baidu')\n",
    "print(\"更新后的列表：\",list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "6ff0874a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['python', 'C', 'Jave', ['Javascript', 'C++', 'Go']]\n",
      "['python', 'C', 'Jave', ['Javascript', 'C++', 'Go'], ['Html', 'SQL']]\n"
     ]
    }
   ],
   "source": [
    "# 追加元组，整个元组被当成一个元素\n",
    "l = ['python','C','Jave']\n",
    "t = ['Javascript','C++','Go']\n",
    "l.append(t)\n",
    "print(l)\n",
    "# 追加列表，整个列表也被当成一个元素\n",
    "l.append(['Html','SQL'])\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c9948ad",
   "metadata": {},
   "source": [
    "## 使用insert()方法插入元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "cf1f54a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['python', 'C', 'C', 'Jave']\n",
      "['python', 'C', ('C#', 'Go'), 'C', 'Jave']\n",
      "['python', 'C', ('C#', 'Go'), ['Ruby', 'SQL'], 'C', 'Jave']\n",
      "['hppt:/c.biancheng.net', 'python', 'C', ('C#', 'Go'), ['Ruby', 'SQL'], 'C', 'Jave']\n"
     ]
    }
   ],
   "source": [
    "l = ['python','C','Jave']\n",
    "# 插入元素\n",
    "l.insert(1,'C')\n",
    "print(l)\n",
    "# 插入元组,整个元组被当成一个元素\n",
    "t = ('C#','Go')\n",
    "l.insert(2,t)\n",
    "print(l)\n",
    "# 插入列表，整个列表被当成一个元素\n",
    "l.insert(3,['Ruby','SQL'])\n",
    "print(l)\n",
    "# 插入整个字符串，整个字符串被当成一个元素\n",
    "l.insert(0,\"hppt:/c.biancheng.net\")\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02f294fd",
   "metadata": {},
   "source": [
    "## extend()方法添加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6d14504e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['python', 'C', 'Jave', 'C']\n",
      "['python', 'C', 'Jave', 'C', 'Javascript', 'C++', 'Go']\n",
      "['python', 'C', 'Jave', 'C', 'Javascript', 'C++', 'Go', 'Ruby', 'SQL']\n"
     ]
    }
   ],
   "source": [
    "l = ['python','C','Jave']\n",
    "# 追加元素\n",
    "l.extend('C')\n",
    "print(l)\n",
    "# 追加元组，元组被拆分为多个元素\n",
    "l.extend(t)\n",
    "print(l)\n",
    "# 追加列表，列表也被拆分为多个元素\n",
    "l.extend(['Ruby','SQL'])\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95b3f43b",
   "metadata": {},
   "source": [
    "##  del:根据索引值删除元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b23ab693",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['python', 'C++', 'PHP', 'Ruby', 'MATLAB']\n",
      "['python', 'C++', 'PHP', 'MATLAB']\n",
      "['python', 'Ruby', 'MATLAB']\n",
      "['python', 'C#', 'Go']\n"
     ]
    }
   ],
   "source": [
    "lang = [\"python\",\"C++\",\"Jave\",\"PHP\",\"Ruby\",\"MATLAB\"]\n",
    "# 使用正数索引\n",
    "del lang[2]\n",
    "print(lang)\n",
    "# 是用负数索引\n",
    "del lang[-2]\n",
    "print(lang)\n",
    "# 是=使用 del 删除一段连续的元素\n",
    "lang = [\"python\",\"C++\",\"Jave\",\"PHP\",\"Ruby\",\"MATLAB\"]\n",
    "del lang[1:4]\n",
    "print(lang)\n",
    "lang.extend([\"SQL\",\"C#\",\"Go\"])\n",
    "del lang[-5:-2]\n",
    "print(lang)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f699de72",
   "metadata": {},
   "source": [
    "## pop()：根据索引删除元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "45674bc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[40, 36, 89, 36, 100, 7]\n",
      "[40, 36, 89, 36, 100]\n"
     ]
    }
   ],
   "source": [
    "nums = [40,36,89,2,36,100,7]\n",
    "nums.pop(3)\n",
    "print(nums)\n",
    "nums.pop()\n",
    "print(nums)\n",
    "# 如果不写 Index 参数，默认会删除列表中最后一个元素，类似数据结构中的“出栈”操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bcf9706",
   "metadata": {},
   "source": [
    "## remove()：根据元素值进行删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ac5af58d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[40, 89, 2, 36, 100, 7]\n",
      "[40, 89, 2, 100, 7]\n"
     ]
    }
   ],
   "source": [
    "nums = [40,36,89,2,36,100,7]\n",
    "# 第一次删除36\n",
    "nums.remove(36)\n",
    "print(nums)\n",
    "# 第二次删除36\n",
    "nums.remove(36)\n",
    "print(nums)\n",
    "# remove() 方法只会删除第一个和指定值相同的元素，而且必须保证该元素是存在的，否则会引发错误"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6db43516",
   "metadata": {},
   "source": [
    "## clear()：删除列表所有的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "29ff60de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "# clear()用来删除列表的所有元素，即清空列表\n",
    "list = [\"python.org\"]\n",
    "url = list\n",
    "url.clear()\n",
    "print(url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46222bf4",
   "metadata": {},
   "source": [
    "## 列表方法补充\n",
    "> 1.list.copy()  复制列表\n",
    "> 2.list.count(obj)  统计某个元素在列表中出现的次数\n",
    "> 3.list.index(obj)  在列表中找到某个值的索引位置"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
